//
//  WY_ScrollView.m
//  Treadmill
//
//  Created by 王跃 on 17/9/29.
//  Copyright © 2017年 王跃. All rights reserved.
//

#import "WY_ScrollButtonView.h"
#define Line_height 2.f
#define Button_width   self.bounds.size.width / self.buttonTitleArray.count
#define Button_height   self.bounds.size.height - Line_height - 10

@interface WY_ScrollButtonView ()
@property (nonatomic, strong) UIImageView *lineImageView;   //底部滑动的线
@property (nonatomic, strong) UIImageView *bottomImageView;   //底部边线
@end


@implementation WY_ScrollButtonView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        self.backgroundColor = [UIColor whiteColor];
        _defaultSelectIndex = 0;
        _tintColor = [UIColor greenColor];
        _defaultcolor = [UIColor whiteColor];
        _bottomLineHidden = YES;
    }
    return self;
}
-(void)setButtonTitleArray:(NSArray *)buttonTitleArray
{
     _buttonTitleArray = buttonTitleArray;
    //选择的button
    for (int i = 0; i < buttonTitleArray.count; i++)
    {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13.f];
        button.tag = 100 + i;
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    //底部边线
    self.bottomImageView = [[UIImageView alloc]init];
    self.bottomImageView.backgroundColor = [UIColor redColor];
    self.bottomImageView.hidden = self.bottomLineHidden;
    [self addSubview:self.bottomImageView];
    
    
    //底部滑动的线
    self.lineImageView = [[UIImageView alloc]init];
    self.lineImageView.backgroundColor = self.tintColor;
    
    [self addSubview:self.lineImageView];

}
//点击后的颜色
-(void)setTintColor:(UIColor *)tintColor
{
    _tintColor = tintColor;
    [self baseSet];
}
//未点击的默认颜色

-(void)setDefaultcolor:(UIColor *)defaultcolor
{
    _defaultcolor = defaultcolor;
    [self baseSet];
}
//线长是否和文字一样
-(void)setIsLengthContainTitle:(BOOL)isLengthContainTitle
{
    _isLengthContainTitle = isLengthContainTitle;
}
//默认哪个下标被选中
-(void)setDefaultSelectIndex:(NSInteger)defaultSelectIndex
{
    _defaultSelectIndex = defaultSelectIndex;
}
-(void)setBottomLineHidden:(BOOL)bottomLineHidden
{
    _bottomLineHidden = bottomLineHidden;
    [self baseSet];
}
-(void)baseSet
{
    
    for (int i = 0; i < self.buttonTitleArray.count; i++)
    {
        UIButton *button = [self viewWithTag:100 + i];
        [button setTitleColor:self.defaultcolor forState:UIControlStateNormal];
        [button setTitleColor:self.tintColor forState:UIControlStateSelected];
    }
    self.lineImageView.backgroundColor = self.tintColor;
    self.bottomImageView.hidden = self.bottomLineHidden;

}
-(void)layoutSubviews
{
     self.bottomImageView.frame = CGRectMake(0, Button_height + Line_height/4, self.frame.size.width,1.f);
    //设置buttonFrame
   
    for (int i = 0; i < self.buttonTitleArray.count; i++)
    {
        UIButton *button = [self viewWithTag:100 + i];
        button.frame = CGRectMake(i * Button_width, 10, Button_width, Button_height);
        [button.titleLabel sizeToFit];
        
        if (i == self.defaultSelectIndex)
        {
            button.selected = YES;
            [button.titleLabel sizeToFit];
            CGFloat lineWidth = self.isLengthContainTitle? button.titleLabel.bounds.size.width : Button_width;
            self.lineImageView.frame = CGRectMake(0, self.height - Line_height, lineWidth + 8, Line_height);
            CGPoint point = self.lineImageView.center;
            point.x = button.center.x;
            self.lineImageView.center = point;
            
        }
        
    }
   
    
}

/*button点击事件*/
-(void)buttonClick:(UIButton *)selectButton
{
    [self ScrollButtonViewDidScrollToIndex:selectButton.tag - 100];
    if (_delete) {
        [_delete buttonSelectedIndex:selectButton.tag - 100];
    }
}

/*实现联动*/
-(void)ScrollButtonViewDidScrollToIndex:(NSInteger)index
{
    for (int i = 0; i < self.buttonTitleArray.count; i++)
    {
        UIButton *button = [self viewWithTag:100 + i];
        button.selected = NO;
    }
    UIButton *selectButton = [self viewWithTag:100 + index];
    selectButton.selected = YES;
    CGPoint point = self.lineImageView.center;
    point.x = selectButton.center.x;
    
    [UIView animateWithDuration:0.1 animations:^{
        self.lineImageView.center = point;
    }];

}
@end
